﻿
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using Company.Database;
using Company.Database.Sqlite;
using Company.Shell.Interfaces;
using Company.Shell.Models;
using Company.Shell.Services;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;

namespace Company.Shell.ViewModels
{
    internal class InitializeViewModel : ObservableObject
    {
        private object? content;
        /// <summary>
        /// 页面
        /// </summary>
        public object? Content
        {
            get { return content; }
            set { SetProperty(ref content, value); }
        }

        private Visibility? showButton = Visibility.Collapsed;
        /// <summary>
        /// 是否显示重试按钮
        /// </summary>
        public Visibility? ShowButton
        {
            get { return showButton; }
            set { SetProperty(ref showButton, value); }
        }
        
        public ICommand? LoadedCommand { get; }
        public ICommand? InitializeCommand { get; }
        private IUserRepository UserRepository { get; }
        private ISession Session { get; }
        private IRoomManager RoomManager { get; }
        private ICollectManager CollectManager { get; }
        private IHardwareMananger HardwareMananger { get; }
        public InitializeViewModel(
            ICollectManager collectManager,
            IRoomManager roomManager,
            IHardwareMananger hardwareMananger,
            ISession session,
            IUserRepository userRepository)
        {
            CollectManager = collectManager;
            RoomManager = roomManager;
            HardwareMananger = hardwareMananger;
            Session = session;
            UserRepository = userRepository;
            LoadedCommand = new AsyncRelayCommand(OnLoadedCommand);
            InitializeCommand = new AsyncRelayCommand(OnInitializeCommand);
            Content = "正在初始化数据库";
        }

        private async Task OnLoadedCommand()
        {
            await Task.Delay(500);
            var guest = UserRepository.Search("guest");
            if (guest == null)
            {
                guest = new Database.Entities.UserEntity
                {
                    UserName = "guest",
                    Password = "12345678",
                    Role = 0
                };
                var success = UserRepository.Insert(guest);
                if (success)
                {
                    //以guest登录，那么，放到会话缓中
                    Session.Add(guest);
                }
            }
            else
            {
                //以guest登录，那么，放到会话缓中
                Session.Add(guest);
            }

            var admin = UserRepository.Search("admin");
            if (admin == null)
            {
                admin = new Database.Entities.UserEntity
                {
                    UserName = "admin",
                    Password = "12345678",
                    Role = 1
                };
                UserRepository.Insert(admin);
            }
            Content = "房间初始化";
            await RoomManager.Initialize();

            Content = "正在连接PLC";
            var result = await HardwareMananger.InitializeAsync();//PLC硬件初始化
            await Task.Delay(500);

            if (result.Success) 
            {
                //todo  开始运行
                CollectManager.Start();
                //todo  跳转首页
                WeakReferenceMessenger.Default.Send(new InitializeSuccessMessage(result.Message));
            }
            else
            {
                //显示重试按钮
                ShowButton = Visibility.Visible;
            }
        }
        /// <summary>
        /// 重新连接PLC
        /// </summary>
        /// <returns></returns>
        private async Task OnInitializeCommand()
        {
            await Task.Delay(500);
            Content = "正在重新连接PLC";
            ShowButton = Visibility.Collapsed;
            var result = await HardwareMananger.InitializeAsync();
            if (result != null)
            {
                Content = result.Message;
            }
        }
    }
}
